fig.path <- "results/graph/"
tab.path <- "results/table/"

Ind$gender<- na_if(Ind$gender, "na")
Ind$female <- (Ind$gender == "female")

### Table 
# Labs <- c("Total Electors", "Local Electors", "Number of Women", 
#     "Share Local over Total Electors" , "Share Women in Local Electors")

Labs <- c("Total Electors (hundreds)",  "Number of Women", 
    "Share Local over Total Electors" )

## Total Electors 
tebase <- Ind %>% group_by(g_name, g_unit, year, treat_gp) %>%
    count() %>% 
    mutate(n100 = n/100)

te <- tebase %>%
    group_by(year, treat_gp) %>% 
    summarize_at(c("n100"), c(mean, sd))

tediff11 <- summary(lm(data = subset(tebase, year == 1911), n100~treat_gp ))
tediff14 <- summary(lm(data = subset(tebase, year == 1914), n100~treat_gp ))

## Local Electors
lebase <- Ind %>% group_by(g_name, g_unit, year, treat_gp) %>%
    subset(type == "occupation division 3") %>%
    count() %>%  
    mutate(n100 = n/100)

le <- lebase %>%
    group_by(year, treat_gp) %>% 
    summarize_at(c("n100"), c(mean, sd))

lediff11 <- summary(lm(data = subset(lebase, year == 1911), n100~treat_gp ))
lediff14 <- summary(lm(data = subset(lebase, year == 1914), n100~treat_gp ))

## N Women  
nwbase <- Ind %>% group_by(g_name, g_unit, year, treat_gp) %>%
    subset(female) %>%
    count()

nw <- nwbase %>% 
    group_by(year, treat_gp) %>% 
    summarize_at(c("n"), c(mean, sd))

nwdiff11 <- summary(lm(data = subset(nwbase, year == 1911), n~treat_gp ))
nwdiff14 <- summary(lm(data = subset(nwbase, year == 1914), n~treat_gp ))

## Share local 

slbase <- Ind %>% group_by(g_name, g_unit, year, treat_gp, type ) %>%
    count( name = "tot_type") %>%
    group_by(g_name, g_unit, year, treat_gp) %>%
    mutate(tot_tot = sum(tot_type)) %>% 
    subset(type == "occupation division 3") %>%
    mutate(share_loc = tot_type/tot_tot)

sl <- slbase %>%
    group_by( year, treat_gp) %>%
    summarize_at(c("share_loc"), c(mean,sd)) 

sldiff11 <- summary(lm(data = subset(slbase, year == 1911), share_loc~treat_gp ))
sldiff14 <- summary(lm(data = subset(slbase, year == 1914), share_loc~treat_gp ))

## Share Women 

##mean

mean11_0  <- round(
  c(te$fn1[te$year == 1911 & te$treat_gp ==0] ,
    # le$fn1[le$year == 1911 & le$treat_gp ==0] ,
    nw$fn1[nw$year == 1911 & nw$treat_gp ==0] , 
    sl$fn1[sl$year == 1911 & sl$treat_gp ==0] 
    # sw$fn1[sw$year == 1911 & sw$treat_gp ==0] 
    )
    , 2 
     )
mean11_1 <- round(
  c(te$fn1[te$year == 1911 & te$treat_gp == 1] ,
    # le$fn1[le$year == 1911 & le$treat_gp == 1] ,
    nw$fn1[nw$year == 1911 & nw$treat_gp == 1] , 
    sl$fn1[sl$year == 1911 & sl$treat_gp == 1] 
    # sw$fn1[sw$year == 1911 & sw$treat_gp == 1] 
    )
    , 2  )

mean14_0  <- round(
  c(te$fn1[te$year == 1914 & te$treat_gp ==0] ,
    # le$fn1[le$year == 1914 & le$treat_gp ==0] ,
    nw$fn1[nw$year == 1914 & nw$treat_gp ==0] , 
    sl$fn1[sl$year == 1914 & sl$treat_gp ==0] 
    # sw$fn1[sw$year == 1914 & sw$treat_gp ==0] 
    )
    , 2  )
mean14_1 <- round(
  c(te$fn1[te$year == 1914 & te$treat_gp == 1] ,
    # le$fn1[le$year == 1914 & le$treat_gp == 1] ,
    nw$fn1[nw$year == 1914 & nw$treat_gp == 1] , 
    sl$fn1[sl$year == 1914 & sl$treat_gp == 1] 
    # sw$fn1[sw$year == 1914 & sw$treat_gp == 1] 
  )
    , 2  
    ) 

## se

sd11_0  <- round(
  c(te$fn2[te$year == 1911 & te$treat_gp ==0] ,
    # le$fn2[le$year == 1911 & le$treat_gp ==0] ,
    nw$fn2[nw$year == 1911 & nw$treat_gp ==0] , 
    sl$fn2[sl$year == 1911 & sl$treat_gp ==0]  
    # sw$fn2[sw$year == 1911 & sw$treat_gp ==0] 
    )
    , 2  
    )
sd11_1  <- round(
  c(te$fn2[te$year == 1911 & te$treat_gp ==1] ,
    # le$fn2[le$year == 1911 & le$treat_gp ==0] ,
    nw$fn2[nw$year == 1911 & nw$treat_gp ==1] , 
    sl$fn2[sl$year == 1911 & sl$treat_gp ==1] 
    # sw$fn2[sw$year == 1911 & sw$treat_gp ==1] 
  )
  , 2  
    )
sd14_0  <- round(
  c(te$fn2[te$year == 1914 & te$treat_gp ==0] ,
    # le$fn2[le$year == 1914 & le$treat_gp ==0] ,
    nw$fn2[nw$year == 1914 & nw$treat_gp ==0] , 
    sl$fn2[sl$year == 1914 & sl$treat_gp ==0] 
    # sw$fn2[sw$year == 1914 & sw$treat_gp ==0] 
    )
    , 2  
    )
sd14_1 <- round(
  c(te$fn2[te$year == 1914 & te$treat_gp == 1] ,
    # le$fn2[le$year == 1914 & le$treat_gp == 1] ,
    nw$fn2[nw$year == 1914 & nw$treat_gp == 1] , 
    sl$fn2[sl$year == 1914 & sl$treat_gp == 1] 
    # sw$fn2[sw$year == 1914 & sw$treat_gp == 1] 
    )
    , 2  
    ) 

## Diff

diff11 <- round(
  c(tediff11$coefficients[2,1],
  nwdiff11$coefficients[2,1],
  sldiff11$coefficients[2,1]
   ),
   2
)

diff14 <- round(
  c(tediff14$coefficients[2,1],
  nwdiff14$coefficients[2,1],
  sldiff14$coefficients[2,1]
   ),
   2
)

# Pvals

pval11 <- round(
  c(
  lediff11$coefficients[2,4],
  nwdiff11$coefficients[2,4],
  sldiff11$coefficients[2,4]
   ),
   2
)

pval14 <- round(
  c(
  lediff14$coefficients[2,4],
  nwdiff14$coefficients[2,4],
  sldiff14$coefficients[2,4]
   ),
   2
)

## Tables
final_tab11 <- as.data.frame(
    cbind(
    Labs,
    mean11_0,
    sd11_0, 
    mean11_1,
    sd11_1,
    diff11,
    pval11)
)
final_tab14 <- as.data.frame(
    cbind(
    Labs,
    mean14_0,
    sd14_0, 
    mean14_1,
    sd14_1,
    diff14, 
    pval14)
)


write.table(final_tab11,
    file=paste0(tab.path, "app_tab_c01_a1.tex"),
    sep=" & ", 
    quote =  FALSE, 
    col.names = FALSE ,
    row.names = FALSE, 
    eol = " \\\\ \n ")
write.table(final_tab14,
    file=paste0(tab.path, "app_tab_c01_a2.tex"),
    sep=" & ", 
    quote =  FALSE, 
    col.names = FALSE ,
    row.names = FALSE, 
    eol = " \\\\ \n ")

rm(list=
    setdiff(ls()[sapply(ls(), function(x) any(class(get(x)) == 'data.frame'))],
    c("Re","Ind")))